/*
 * @lc app=leetcode.cn id=557 lang=typescript
 *
 * [557] 反转字符串中的单词 III
 */

// @lc code=start
function reverseWords(s: string): string {
    let stack: string[] = [];
    let final = [];
    for (const iterator of s) {
        if (iterator === " ") {
            const single = stack.join("");
            stack = [];
            final.push(single);
        } else {
            stack.unshift(iterator);
        }
    }
    if (stack.length) {
        const single = stack.join("");
        final.push(single);
    }
    return final.join(" ");
}
// @lc code=end
